package com.sikiedu.logindemo2.controller;

import com.sikiedu.logindemo2.domain.User;
import com.sikiedu.logindemo2.service.UserService;
import com.sikiedu.logindemo2.utils.CommonResult;
import com.sikiedu.logindemo2.utils.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@CrossOrigin
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public CommonResult<Message> login(@RequestBody User user){
        boolean success=userService.login(user);
        if(success){
            return CommonResult.successResult(Message.createMessage("登录成功！"));
        }else {
            return CommonResult.failResult(2,Message.createMessage("用户名或密码错误！"));
        }
    }

    @RequestMapping("/register")
    @ResponseBody
    public CommonResult<Message> register(@RequestBody User user){
        boolean success=userService.register(user);
        if(success){
            return CommonResult.successResult(Message.createMessage("注册成功！"));
        }else {
            return CommonResult.failResult(1,Message.createMessage("用户已存在！"));
        }
    }

}
